package com.bwf.tcp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * @Version v1.0
 * @Description: TODO
 * @Author ZHANZHONGXIN
 * @Date 2022/5/26 13:45
 * @Copyright 博为峰
 */
public class ServerDemo {
    public static void main(String[] args) throws IOException {
        /*
           TCP:服务端
           1.创建tcp服务端socket服务
         */
        ServerSocket serverSocket = new ServerSocket(9999);
        Socket socket = serverSocket.accept();//阻塞
        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int i = inputStream.read(bytes);
        String str = new String(bytes, 0, i);
        System.out.println("收到的数据 = " + str);

        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("收到".getBytes(StandardCharsets.UTF_8));

        socket.close();
        serverSocket.close();
    }
}
